package com.ace.log.file.name

import java.text.SimpleDateFormat
import java.util.*

/**
 * 每隔一定的小时数对日志进行分片
 */
class DateWithHourFileNameGenerator(val divider: Int) : IFileNameGenerator {

    private val mLocalDateFormat: ThreadLocal<SimpleDateFormat> =
        object : ThreadLocal<SimpleDateFormat>() {

            override fun initialValue() = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        }

    override fun isFileNameChangeable() = true

    override fun generateFileName(
        logLevel: Int,
        tag: String,
        timestamp: Long,
        lastFileName: String
    ): String {

        val sdf = mLocalDateFormat.get() ?: SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)

        return sdf.let {
            it.timeZone = TimeZone.getDefault()
            val date = Date(timestamp)
            val dateString = it.format(date)
            "$dateString-${date.hours / divider}"
        }


    }
}